$(function(){
	var menus = [
		{
			menu: 'factoryConfiguration',
			text: '系统管理',
			children: [
				{
					menu: 'systemFactory',
					text: '工厂',
					url: '/management/factory/pageList'
				},
				{
					menu: 'functionGroup',
					text: '功能组',
					url: '/management/functionGroup/listFunctionGroupsPageByCondition'
				},
				{
					menu: 'systemLanguage',
					text: '语言',
					url: '/management/language/language_list'
				},
				{
					menu: 'enumCodeManage',
					text: '枚举码维护',
					url: '/management/adminECode/listECodeTypesPageByCondition'
				},
				{
					menu: 'enumCodeTranslate',
					text: '枚举码翻译',
					url: '/management/adminECode/beforeTranslate'
				},
				{
					menu: 'resource',
					text: '资源',
					url: '/management/resource/resourceList'
				},
				{
					menu: 'timejob',
					text: '定时作业',
					url: '/management/timejob/timeJobList'
				}
			]
		}
	],
	$page = $(document),
	$menuPanel = $('#menu', $page).addClass('uiComplete'),
	baseUrl = $menuPanel.attr('base'),
	appendChildrenMenu = function($ul, cMenus) {
		$.each(cMenus, function(){
			var url = this.url, text = this.text, rel = this.menu,
			$a = $('<a/>'),
			$li = $('<li/>').append($a.attr({
				rel: rel
			}).text(text));
			if (this.url && !this.children) {
				$a.attr({
					target: 'navTab',
					href: baseUrl + url + (url.indexOf('?') > 0 ? '&' : '?') + 'navTabId=' + rel,
					rel: rel
				});
			}
			if (this.children && this.children.length) {
				var $cUl = $('<ul/>');
				appendChildrenMenu($cUl, this.children);
				$li.append($cUl);
			}
			$ul.append($li);
		});
	},
	getContentHeight = function() {
		var $sidebar = $('#sidebar', $page), $menu = $('>.accordionHeader:first', $menuPanel),
		menuHeight = $sidebar.height() - $('>.toggleCollapse:first', $sidebar).height(),
		contentHeight = menuHeight - $menu.height() * $('>.accordionHeader', $menuPanel).length;
		return contentHeight - 10;
	};
	
	$.each(menus, function(){
		var url = this.url, rel = this.menu, text = this.text, $menu = $('<div class="accordionHeader" />').append('<h2><span>Folder</span>' + this.text + '</h2>');
		$menuPanel.append($menu);
		if (this.children && !this.url) {
			var $ul = $('<ul class="treeMenu treeFolder"/>').appendTo($('<div class="accordionContent" style="display: none;"/>').appendTo($menuPanel));
			appendChildrenMenu($ul, this.children);
		} else {
			$menu.addClass('nomenu').attr({
				target: 'navTab',
				rel: rel,
				href: baseUrl + url,
				text: text
			});
			$menuPanel.append('<div class="accordionContent" style="display: none;"/>');
		}
	});
	
	$menuPanel.on('uiComplete', function(){
		var $menu = $('.accordionHeader:not(.nomenu):first', $menuPanel);
		$menu.next('.accordionContent:first').css({height: getContentHeight()}).show();
		$('>h2', $menu).addClass('collapsable');
	});
	
	$('.nomenu', $menuPanel).off('click').on('click', function(event){
		var $this = $(this), url = $this.attr('href');
		if (url && url != baseUrl) {
			navTab.openTab($this.attr('rel'), $this.attr('href'), {title: $this.attr('text'), fresh: true, external: false});
		}
		return false;
	});
});